package com.pearlsoft.webapp.rms.resourcesMonitor;

import java.util.ArrayList;
import java.util.List;

import com.pearlsoft.rms.MetricResult;
import com.pearlsoft.rms.VcenterMonitor;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;

@QueryHandlerComponent(name = "RealtimeResourcesMonitor")
public class RealtimeResourcesMonitor extends AbsBaseQueryHandler {

    /*
     * 获取CPU实时信息
     */
    public void getRealtimeCpuInfo(Query query, DataSet ds) throws Exception {
        String vmname = (String) query.get("vmname");
        String counterName = (String) query.get("counterName");
        int interval = Integer.parseInt((String) query.get("interval"));
        VcenterMonitor vcm = this.getBean("vcenterMonitor", VcenterMonitor.class);
        MetricResult result = vcm.getRealtimeCpuInfo(vmname, counterName, interval);
        List<Record> list = new ArrayList<Record>();
        int i = 1;
        if (!result.getData().isEmpty()) {
            for (Long listCpu : result.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("drawTime", i++);
                recrod.setValue("infoPoint", listCpu);
                recrod.setValue("unit", result.getUnit());
                list.add(recrod);
            }
        }
        ds.setRecords(list);
    }

    /*
     * 获取内存实时信息
     */
    public void getRealtimeMemInfo(Query query, DataSet ds) throws Exception {
        String vmname = (String) query.get("vmname");
        String counterName = (String) query.get("counterName");
        int interval = Integer.parseInt((String) query.get("interval"));
        VcenterMonitor vcm = this.getBean("vcenterMonitor", VcenterMonitor.class);
        MetricResult result = vcm.getRealtimeMemInfo(vmname, counterName, interval);
        List<Record> list = new ArrayList<Record>();
        int i = 1;
        if (!result.getData().isEmpty()) {
            for (Long listMem : result.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("drawTime", i++);
                recrod.setValue("infoPoint", listMem);
                recrod.setValue("unit", result.getUnit());
                list.add(recrod);
            }
        }
        ds.setRecords(list);
    }

    /*
     * 获取硬盘实时信息
     */
    public void getRealtimeDiskInfo(Query query, DataSet ds) throws Exception {
        String vmname = (String) query.get("vmname");
        String counterName = (String) query.get("counterName");
        int interval = Integer.parseInt((String) query.get("interval"));
        VcenterMonitor vcm = this.getBean("vcenterMonitor", VcenterMonitor.class);
        MetricResult result = vcm.getRealtimeDiskInfo(vmname, counterName, interval);
        List<Record> list = new ArrayList<Record>();
        int i = 1;
        if (!result.getData().isEmpty()) {
            for (Long listDisk : result.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("drawTime", i++);
                recrod.setValue("infoPoint", listDisk);
                recrod.setValue("unit", result.getUnit());
                list.add(recrod);
            }
        }
        ds.setRecords(list);
    }

    /*
     * 获取所有实时信息
     */
    public void getRealtimeAllInfo(Query query, DataSet ds) throws Exception {
        String vmname = (String) query.get("vmname");
        String counterName = (String) query.get("counterName");
        int interval = Integer.parseInt((String) query.get("interval"));
        VcenterMonitor vcm = this.getBean("vcenterMonitor", VcenterMonitor.class);
        MetricResult diskResult = vcm.getRealtimeDiskInfo(vmname, counterName, interval);
        MetricResult memResult = vcm.getRealtimeMemInfo(vmname, counterName, interval);
        MetricResult cpuResult = vcm.getRealtimeCpuInfo(vmname, counterName, interval);
        List<Record> list = new ArrayList<Record>();
        int i = 1;
        if (!diskResult.getData().isEmpty()) {
            for (Long listDisk : diskResult.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("drawTime", i++);
                recrod.setValue("type", "disk");
                recrod.setValue("infoPoint", listDisk);
                recrod.setValue("unit", diskResult.getUnit());
                list.add(recrod);
            }
        }
        i = 1;
        if (!memResult.getData().isEmpty()) {
            for (Long listMem : memResult.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("drawTime", i++);
                recrod.setValue("type", "memory");
                recrod.setValue("infoPoint", listMem);
                recrod.setValue("unit", memResult.getUnit());
                list.add(recrod);
            }
        }
        i = 1;
        if (!cpuResult.getData().isEmpty()) {
            for (Long listCpu : cpuResult.getData().get(0)) {
                SimpleRecord recrod = new SimpleRecord();
                recrod.setValue("type", "cpu");
                recrod.setValue("drawTime", i++);
                recrod.setValue("infoPoint", listCpu);
                recrod.setValue("unit", cpuResult.getUnit());
                list.add(recrod);
            }
        }
        ds.setRecords(list);
    }

    @Override
    public void advSearch(Query query, DataSet ds) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void getEntity(Query query, DataSet ds) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void quickSearch(Query query, DataSet ds) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void handler(Query query, DataSet ds) throws Exception {
        // TODO Auto-generated method stub

    }

}
